import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-11-07
 * Time: 16:46
 */
public class Test {

    public static void main3(String[] args) {
        Set<String> set = new TreeSet<>();

        set.add("hello");
        set.add("world");
        set.add("abcd");

        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

        System.out.println(set.contains("hello"));  // true

        System.out.println(set.remove("abcd"));  //true

        System.out.println(set.size());    // 2

        System.out.println(set.containsAll(set));   //true
    }

    public static void main1(String[] args) {

        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "value1");
        map.put("2", "value2");
        map.put("3", "value3");

        //第一种：普遍使用，二次取值,效率较低
        System.out.println("通过Map.keySet遍历key和value：");
        for (String key : map.keySet()) {
            System.out.println("key= "+ key + " and value= " + map.get(key));
        }

        //第二种
        System.out.println("通过Map.entrySet使用iterator遍历key和value：");
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }

        //第三种：推荐，尤其是容量大时，效率较高
        System.out.println("通过Map.entrySet遍历key和value");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }

        //第四种
        System.out.println("通过Map.values()遍历所有的value，但不能遍历key");
        for (String v : map.values()) {
            System.out.println("value= " + v);
        }
    }
    public static void main(String[] args) {
        Map<String, String> map = new TreeMap();

        //Map<String, Integer> map1 = new HashMap<>();

        map.put("Key1", "Value1");
        map.put("Key2", "Value2");
        map.put("Key3", "Value3");
        map.put("Key4", "Value4");
        map.put("Key5", "Value5");

        System.out.println(map.getOrDefault("aaa", "nothing"));  //nothing

        map.put("Key1", "aaa");           //修改 Key1 的值为aaa

        Set<String> set = map.keySet();
        System.out.println(set);          //[Key1, Key2, Key3, Key4, Key5]

        Collection<String> collection = map.values();
        System.out.println(collection);              //[Value1, Value2, Value3, Value4, Value5]

        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }
    }

}
